<html>
<head><meta charset="utf-8"><title>Can `T: &#x27;static` really never point to the stack? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html">Can `T: &#x27;static` really never point to the stack?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="165829309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165829309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165829309">(May 16 2019 at 17:19)</a>:</h4>
<p>See <a href="https://github.com/japaric/microamp/issues/6" target="_blank" title="https://github.com/japaric/microamp/issues/6">https://github.com/japaric/microamp/issues/6</a>: we have some unsafe code that relies on <code>T: 'static</code> types never having stack pointers. I think this is far from obvious. Is it something we want to guarantee?</p>



<a name="165830074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165830074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165830074">(May 16 2019 at 17:28)</a>:</h4>
<p>We have that same problem in CUDA</p>



<a name="165830104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165830104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165830104">(May 16 2019 at 17:28)</a>:</h4>
<p>And the safest bet was not to use references</p>



<a name="165830158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165830158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165830158">(May 16 2019 at 17:29)</a>:</h4>
<p>Reference implies dereferenceable, but if you can access a static from, e.g., a different core, where addresses map to different memory, that reference is not dereferenceable</p>



<a name="165830310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165830310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165830310">(May 16 2019 at 17:30)</a>:</h4>
<p>AFAICT one does not need to access the reference to trigger the UB, the mere possibility of being able to access it is UB</p>



<a name="165830400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165830400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165830400">(May 16 2019 at 17:31)</a>:</h4>
<p>in other words, if you have a target that has "segmented" memory (e.g.  where some cores can only access some memory), putting addresses to the segment into static references that can be read by code that does not have access to the segment is UB - you have to use raw pointers instead</p>



<a name="165831063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165831063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165831063">(May 16 2019 at 17:38)</a>:</h4>
<p>This is like being able to write code like this:</p>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">GLOBAL</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="kt">u32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rust</span><span class="w"></span>

<span class="w">    </span><span class="c1">// some thread:</span>
<span class="w">    </span><span class="n">thread_local</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">pub</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">LOCAL</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">LOCAL</span><span class="p">.</span><span class="n">with</span><span class="p">(</span><span class="o">|</span><span class="n">f</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">GLOBAL</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>

<span class="w">    </span><span class="c1">// some other thread can access GLOBAL</span>
<span class="w">    </span><span class="c1">// but GLOBAL is not dereferenceable from that thread</span>
</pre></div>


<p>Is this UB, even if no other thread actually access GLOBAL, just from being able to access it?</p>



<a name="165834800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165834800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165834800">(May 16 2019 at 18:17)</a>:</h4>
<p>so basically, do we have <code>derefencable</code> on globals? good question. I don't know.</p>



<a name="165834863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165834863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165834863">(May 16 2019 at 18:17)</a>:</h4>
<p>but the problem here is a different one</p>



<a name="165835118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165835118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165835118">(May 16 2019 at 18:20)</a>:</h4>
<p>There was also talk in the past about considering the locals of <code>main</code> to be loanable for <code>'static</code> if panics abort (if it unwinds, I'm pretty sure it's unsound, but I forget the actual argument)</p>



<a name="165835294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165835294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165835294">(May 16 2019 at 18:22)</a>:</h4>
<p>Ah no, it was functions that loop infinitely, i.e., those that diverge not because of panics but because of control flow. (<a href="https://internals.rust-lang.org/t/set-static-lifetime-for-local-variables-of-diverging-functions/9649" target="_blank" title="https://internals.rust-lang.org/t/set-static-lifetime-for-local-variables-of-diverging-functions/9649">https://internals.rust-lang.org/t/set-static-lifetime-for-local-variables-of-diverging-functions/9649</a>) so basically the example Ralf gave in the issue linked at the start.</p>



<a name="165835717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165835717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165835717">(May 16 2019 at 18:27)</a>:</h4>
<p>(and indeed my example as given is unsound with panic=unwind, but that can be fixed by moving the loop to a drop guard)</p>



<a name="165838510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165838510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165838510">(May 16 2019 at 18:57)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> well here you don't have globals, just whether the assumption that <code>T: 'static</code> implies no references to the stack or nor</p>



<a name="165838629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165838629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165838629">(May 16 2019 at 18:58)</a>:</h4>
<p>AFAICT it doesn't, but I don't see how that matters, because the case being considered here (segmented memory) is one that we don't support in the "references" part of the type system at all AFAICT</p>



<a name="165838769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165838769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165838769">(May 16 2019 at 18:59)</a>:</h4>
<p>you can create a reference to a thread stack (or its heap), and as long as that reference lives long enough, pass it to another thread</p>



<a name="165838865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165838865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165838865">(May 16 2019 at 19:00)</a>:</h4>
<p>if that other thread cannot dereference that address because of "reasons" (e.g. the address space associated with it is a different one), well, that's UB</p>



<a name="165839074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839074">(May 16 2019 at 19:02)</a>:</h4>
<p>the unsafe code that creates that thread, and allows you to pass it non-dereferenceable references is at fault</p>



<a name="165839206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839206">(May 16 2019 at 19:03)</a>:</h4>
<p>well it doesnt imply no references (because it can be refs to statics). but it might imply no references <em>to the stack</em>.</p>



<a name="165839324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839324">(May 16 2019 at 19:04)</a>:</h4>
<p>Can't you heap allocate something, create a &amp;'static to it, and leak it ? (isn't that safe?)</p>



<a name="165839373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839373">(May 16 2019 at 19:05)</a>:</h4>
<p>and then its heap allocated and not stack allocated...?</p>



<a name="165839414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839414">(May 16 2019 at 19:05)</a>:</h4>
<p>yeah, and then instead of a thread that cannot access some other thread stack because of "reasons", you have threads that cannot access some other thread heap because of "reasons"</p>



<a name="165839531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839531">(May 16 2019 at 19:07)</a>:</h4>
<p>we could call these thread "processes", and that would be alright</p>



<a name="165839568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/165839568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#165839568">(May 16 2019 at 19:07)</a>:</h4>
<p>but it could also be the CPU heap, and a thread of execution on the GPU, which can only access the GPUs heap</p>



<a name="166163722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166163722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166163722">(May 21 2019 at 11:35)</a>:</h4>
<p>have you read the issue I linked above? this is specifically about the stack.</p>



<a name="166277173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166277173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166277173">(May 22 2019 at 15:11)</a>:</h4>
<p>the stack of different threads of execution</p>



<a name="166277406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166277406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166277406">(May 22 2019 at 15:13)</a>:</h4>
<p>where each thread can only access its own stack</p>



<a name="166277523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166277523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166277523">(May 22 2019 at 15:14)</a>:</h4>
<p>i don't see how this is any different for an application where each thread has its own thread-local heap, and you can't access anything on that heap from a different thread</p>



<a name="166277807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166277807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166277807">(May 22 2019 at 15:17)</a>:</h4>
<p>is there anything that makes the stack here special as opposed to a thread-local variable ?</p>



<a name="166304299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166304299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166304299">(May 22 2019 at 20:03)</a>:</h4>
<p>yes -- that in safe Rust + libstd it is not possible to get a <code>'static</code> reference to the stack, while <code>Box::leak</code> <em>does</em> provide a safe way to get a <code>'static</code> to the heap</p>



<a name="166304362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166304362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166304362">(May 22 2019 at 20:04)</a>:</h4>
<p>so for the stack it <em>seems</em> like <code>T: 'static</code> successfully excludes such references</p>



<a name="166304396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166304396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166304396">(May 22 2019 at 20:04)</a>:</h4>
<p>but I think that is an "accident" and we should not endorse such a pattern. at the same time there is clearly a need and I dont know if we have an answer.</p>



<a name="166373481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166373481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166373481">(May 23 2019 at 15:48)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> FYI by thread-local heap i actually mean literally that. As in, you spawn a couple of threads, and give each a <code>thread_local! { HEAP }</code> (what <code>HEAP</code> actually is does not matter much, it could be just a Box that gets destroyed when the thread joins). The API of thread-local will ensure that you can't let references into your thread-local heap escape.</p>



<a name="166373517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166373517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166373517">(May 23 2019 at 15:49)</a>:</h4>
<p>I think there is not much difference between using <code>thread_local!</code> in that way, and what Jorge is doing</p>



<a name="166373547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166373547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166373547">(May 23 2019 at 15:49)</a>:</h4>
<p>the issue is that the assumption that <code>T: 'static</code> prevents references to the stack is incorrect.</p>



<a name="166373660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166373660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166373660">(May 23 2019 at 15:50)</a>:</h4>
<p>while the API of <code>thread_local!</code> prevents letting references escape using other means</p>



<a name="166374326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166374326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166374326">(May 23 2019 at 15:57)</a>:</h4>
<p>ah, <code>T: 'static</code> also "prevents" (or rather, does not) references to thread-local data</p>



<a name="166376845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20%60T%3A%20%27static%60%20really%20never%20point%20to%20the%20stack%3F/near/166376845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20.60T.3A.20&#x27;static.60.20really.20never.20point.20to.20the.20stack.3F.html#166376845">(May 23 2019 at 16:26)</a>:</h4>
<p>I mean, if two threads run on different cores, and they can't dereference pointers to each other stack frames, then their stack frames are essentially "thread-local"</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>